#ifndef MYSQL_POOL_H
#define MYSQL_POOL_H

#include <mysql/mysql.h>
#include <string>
#include <vector>
#include <queue>
#include <memory>
#include <mutex>
#include <atomic>
#include <condition_variable>

namespace database {

// MySQL数据库连接
class MySQLConnection {
public:
    MySQLConnection();
    ~MySQLConnection();

    // 连接到数据库
    bool connect(const std::string& host, const std::string& user, const std::string& passwd,
                 const std::string& db, unsigned int port = 3306);

    // 执行SQL查询
    bool query(const std::string& sql);

    // 获取查询结果
    MYSQL_RES* getResult();

    // 获取插入的自增ID
    uint64_t getInsertId();

    // 获取影响的行数
    uint64_t getAffectedRows();

    // 开始事务
    bool startTransaction();

    // 提交事务
    bool commit();

    // 回滚事务
    bool rollback();

    // 获取错误信息
    std::string getError();

    // 关闭连接
    void close();

    // 检查连接是否可用
    bool ping();

private:
    MYSQL* mysql_;
};

// MySQL连接池
class MySQLPool {
public:
    // 创建连接池实例
    static std::shared_ptr<MySQLPool> create(const std::string& host, const std::string& user,
                                           const std::string& passwd, const std::string& db,
                                           unsigned int port = 3306, size_t poolSize = 10);

    // 获取数据库连接
    std::shared_ptr<MySQLConnection> getConnection(int timeoutMs = 5000);

    // 释放数据库连接（重新放回池中）
    void releaseConnection(std::shared_ptr<MySQLConnection> conn);

    // 关闭连接池
    void close();

    // 获取池大小
    size_t getPoolSize() const { return poolSize_; }

    // 获取当前可用连接数
    size_t getAvailableCount();

    // 获取当前使用连接数
    size_t getUsedCount();

    ~MySQLPool();

private:
    // 私有构造函数
    MySQLPool(const std::string& host, const std::string& user, const std::string& passwd,
              const std::string& db, unsigned int port, size_t poolSize);

    // 创建新连接
    std::shared_ptr<MySQLConnection> createConnection();

private:
    std::string host_;
    std::string user_;
    std::string passwd_;
    std::string db_;
    unsigned int port_;
    size_t poolSize_;

    std::queue<std::shared_ptr<MySQLConnection>> connQueue_;
    std::mutex mutex_;
    std::condition_variable condition_;
    std::atomic<bool> isRunning_;
    std::atomic<size_t> createdCount_;
};

// 自动归还的连接类
class ConnectionGuard {
public:
    ConnectionGuard(std::shared_ptr<MySQLPool> pool, std::shared_ptr<MySQLConnection> conn)
        : pool_(pool), conn_(conn) {}

    ~ConnectionGuard() {
        if (pool_ && conn_) {
            pool_->releaseConnection(conn_);
        }
    }

    MySQLConnection* operator->() {
        return conn_.get();
    }

    // 禁止复制
    ConnectionGuard(const ConnectionGuard&) = delete;
    ConnectionGuard& operator=(const ConnectionGuard&) = delete;

private:
    std::shared_ptr<MySQLPool> pool_;
    std::shared_ptr<MySQLConnection> conn_;
};

// 获取自动归还的连接
inline ConnectionGuard getGuardConnection(std::shared_ptr<MySQLPool> pool, int timeoutMs = 5000) {
    return ConnectionGuard(pool, pool->getConnection(timeoutMs));
}

} // namespace database

#endif // MYSQL_POOL_H 